package com.jingdianjichi.subject.domain.handler.subject;

import com.jingdianjichi.subject.common.enums.SubjectTypeEnum;

import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
public class SubjectTypeFactory implements InitializingBean {

    @Resource
    List<SubjectTypeHandler> subjectTypeHandlers;

    private Map<SubjectTypeEnum, SubjectTypeHandler> map = new HashMap<>();

    public SubjectTypeHandler getHandler(int type){
        SubjectTypeHandler subjectTypeHandler = map.get(SubjectTypeEnum.getByCode(type));
        return subjectTypeHandler;
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        for (SubjectTypeHandler subjectTypeHandler : subjectTypeHandlers) {
            map.put(subjectTypeHandler.getSubjectType(), subjectTypeHandler);
        }
    }
}
